package com.szsh.aiot.hsm.mapper;

import com.szsh.aiot.hsm.dto.plugin.IPluginInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface IPluginInfoMapper {

    @Select({"<script>",
        "select * from I_PLUGIN_INFO where gatewayModel=#{gatewayModel}",
        "<if test ='chipModel != null and chipModel !=\"\"'> ",
            " and chipModel=#{chipModel} and ischipModel=1",
        "</if>",
        " and identity=#{identity}",
        " order by pluginVersion desc limit 1",
    "</script>",})
    IPluginInfo getPluginInfo(@Param("gatewayModel") String gatewayModel,
                              @Param("chipModel") String chipModel, @Param("identity") int identity);

    @Select({"<script>",
            "select * from I_PLUGIN_INFO ",
            "where gatewayModel=#{devModel} and identity=#{identity}",
            "</script>" })
    List<IPluginInfo> getPluginInfoList(@Param("devModel") String devModel,
            @Param("identity") int identity);
    /**
     * 新增插件
     *
     * @param param
     * @return
     */
    @Insert({ "insert into I_PLUGIN_INFO( gatewayModel,gatewayType,vendor,chipModel,pluginName,pluginVersion,identity,ischipModel)",
            " values (#{gatewayModel,jdbcType=VARCHAR}, #{gatewayType,jdbcType=VARCHAR},",
            "#{vendor,jdbcType=VARCHAR},#{chipModel,jdbcType=VARCHAR},#{pluginName,jdbcType=VARCHAR},#{pluginVersion,jdbcType=VARCHAR},",
            "#{identity,jdbcType=INTEGER},#{ischipModel,jdbcType=INTEGER}",
            ")" })
    int insert(IPluginInfo param);

    @Delete({
            "delete from I_PLUGIN_INFO ",
            " where serno = #{serno,jdbcType=INTEGER}"
    })
    int delete(@Param("serno") String serno);


    /**
     * 更新插件信息
     *
     * @param param
     * @return
     */
    @Update({
            "update I_PLUGIN_INFO set gatewayModel=#{gatewayModel},gatewayType=#{gatewayType},",
            "vendor=#{vendor},chipModel=#{chipModel},pluginName=#{pluginName},",
            "pluginVersion=#{pluginVersion},identity=#{identity},ischipModel=#{ischipModel} ",
            "where serno=#{serno}",
    })
    int update(IPluginInfo param);


    /**
     * 分页查询
     * @param gatewayModel
     * @param page
     * @param limit
     * @return
     */
    @Select({ "<script>",
            "SELECT * from I_PLUGIN_INFO ",
                "<where>",
                    "<if test ='gatewayModel != null and gatewayModel !=\"\"'> ",
                        " and gatewayModel like concat('%',#{gatewayModel},'%') ",
                    "</if>",
                "</where>",
            " limit #{page}, #{limit}",
            "</script>" })
    List<IPluginInfo> queryPaginationData(@Param("gatewayModel")String gatewayModel, @Param("page") int page, @Param("limit")int limit);


    /**
     * 插件总数查询
     * @param gatewayModel
     * @return
     */
    @Select({ "<script>",
            "SELECT count(*) from I_PLUGIN_INFO ",
                "<where>",
                    "<if test ='gatewayModel != null and gatewayModel !=\"\"'> ",
                        " and gatewayModel like concat('%',#{gatewayModel},'%') ",
                    "</if>",
                "</where>",
            "</script>" })
    Integer getCount(@Param("gatewayModel") String gatewayModel);


    @Select({
            "select * from I_PLUGIN_INFO where serno=#{serno} " })
    IPluginInfo getBySerno(@Param("serno")String serno);


}
